package com.loveto.service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.loveto.common.ImageDownloader;
import com.loveto.common.JsonUtils;
import com.loveto.common.SoftKeyboardDetectorView;
import com.loveto.common.SoftKeyboardDetectorView.OnHiddenKeyboardListener;
import com.loveto.common.SoftKeyboardDetectorView.OnShownKeyboardListener;
import com.loveto.constant.LovetoConstant;

public class UserinfoEditMainActivity extends BaseActivity implements
View.OnClickListener, OnItemSelectedListener {
	
	// 사용하는 변수들
	private Intent intentMoveToPage;
	private SharedPreferences pref;
	private Handler myHandler = new Handler();
	private ProgressDialog progressDialog;
	private String memberid;
	private String local;
	private String local_detail;
	private String age;
	private String job;
	private String height;
	private String my_gender;
	private Spinner spinnerLocal;
	private Spinner spinnerLocalDetail;
	private String[] arrLocal; 
	private String[] arrLocalDetail; 
	private ArrayAdapter<String> localAdapter;
	private ArrayAdapter<String> localDetailAdapter;
	private int presentLocalNum;
	private String beforeLocalName;
	private EditText v_userinfo_introduce;
	private EditText v_userinfo_age;
	private EditText v_userinfo_job;
	private EditText v_userinfo_height;
	private boolean isShowKeyboard;
	private SoftKeyboardDetectorView softKeyboardDetector; 
	private boolean isSaveSuccess;
	private String errorMsg;
	private String suspend_date;
	private String suspend_reason;
	private String phone_uid;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loveto_userinfo_edit_main); 
		
		//변수 선언
		progressDialog = new ProgressDialog(this);
		presentLocalNum = 0;
		beforeLocalName = "";
		pref = getSharedPreferences("userInfo", Activity.MODE_PRIVATE);
		v_userinfo_introduce = (EditText) findViewById(R.id.userinfo_introduce);
		v_userinfo_age = (EditText) findViewById(R.id.userinfo_age);
		v_userinfo_job = (EditText) findViewById(R.id.userinfo_job);
		v_userinfo_height = (EditText) findViewById(R.id.userinfo_height);
		isSaveSuccess = false;
		errorMsg = "";
		
		//로컬에서 정보 받아오기
		memberid = pref.getString("memberid", "");
		my_gender = pref.getString("gender", "");
		phone_uid = pref.getString("phone_uid", "");
		
		//스피너 기본 세팅
		arrLocal = LovetoConstant.LOCAL_NAME;
		arrLocalDetail = LovetoConstant.LOCALDETAIL_CHOONGBOOK; // 널포인트 에러를 피하기 위해 일단 넣어줌.
		spinnerLocal = (Spinner) findViewById(R.id.userinfo_local);
		spinnerLocalDetail = (Spinner) findViewById(R.id.userinfo_local_detail);
		spinnerLocal.setPrompt("지역을 선택하세요.");
		spinnerLocalDetail.setPrompt("상세지역을 선택하세요.");
		localAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,arrLocal); //스피너 어댑터
		localAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerLocal.setAdapter(localAdapter);
		localAdapter.notifyDataSetChanged();
		spinnerLocalDetail.setVisibility(Spinner.GONE); //일단 숨김.
		// 셀렉트 리스너 달기 // 스피너
		spinnerLocal.setOnItemSelectedListener(this);
		
		// 클릭 리스너 달기 // 버튼
		((TextView)findViewById(R.id.btn_save_userinfo)).setOnClickListener(this);
		ImageView v_btn_save_userinfo_under = (ImageView)findViewById(R.id.btn_save_userinfo_under);
		v_btn_save_userinfo_under.setOnClickListener(this);
		TextView v_userinfo_characters = (TextView)findViewById(R.id.userinfo_characters);
		v_userinfo_characters.setOnClickListener(this);
		TextView v_userinfo_look = (TextView)findViewById(R.id.userinfo_look);
		v_userinfo_look.setOnClickListener(this);
		TextView v_userinfo_hobby = (TextView)findViewById(R.id.userinfo_hobby);
		v_userinfo_hobby.setOnClickListener(this);
		TextView v_userinfo_favorite = (TextView)findViewById(R.id.userinfo_favorite);
		v_userinfo_favorite.setOnClickListener(this);
		TextView v_userinfo_idealtype = (TextView)findViewById(R.id.userinfo_idealtype);
		v_userinfo_idealtype.setOnClickListener(this);
		RelativeLayout v_btn_show_album = (RelativeLayout)findViewById(R.id.btn_show_album);
		v_btn_show_album.setOnClickListener(this);
		RelativeLayout v_btn_goto_back = (RelativeLayout)findViewById(R.id.btn_goto_back);
		v_btn_goto_back.setOnClickListener(this);
		((ImageView)findViewById(R.id.userinfo_photo_01)).setOnClickListener(this);
		((ImageView)findViewById(R.id.userinfo_photo_02)).setOnClickListener(this);
		((ImageView)findViewById(R.id.userinfo_photo_03)).setOnClickListener(this);
		((ImageView)findViewById(R.id.userinfo_photo_04)).setOnClickListener(this);
		
		//배경 성별에 따른 처리
		String strGender = "여성";
		if ("M".equals(my_gender)) {
			strGender = "남성";
			RelativeLayout v_userinfo_bg = (RelativeLayout) findViewById(R.id.userinfo_bg);
			v_userinfo_bg.setBackgroundResource(R.drawable.bg_user_man);
		}
		TextView v_userinfo_gender = (TextView) findViewById(R.id.userinfo_gender);
		v_userinfo_gender.setText(strGender);
		
		//유저의 나머지 정보 세팅
		getUserInfo();
		setUserPhoto();
		
		//키보드가 나왔는지 학인 처리 , AndroidManifest.xml에서 android:windowSoftInputMode 가 adjustResize 속성일 때만 가능.
		isShowKeyboard = false; //키보드 상태를 파악하기 위한
		softKeyboardDetector = new  SoftKeyboardDetectorView(this); //키보드가 나왔는지 확인하기 위한
		addContentView(softKeyboardDetector, new FrameLayout.LayoutParams(-1, -1));
		softKeyboardDetector.setOnShownKeyboard(new OnShownKeyboardListener() {
			@Override
			public void onShowSoftKeyboard() {
				isShowKeyboard = true;
				Log.d("tag", "키보드가 떴습니당~");
			}
		});
		softKeyboardDetector.setOnHiddenKeyboard(new OnHiddenKeyboardListener() {
			@Override
			public void onHiddenSoftKeyboard() {
	        	reActiveButtons(); //다시 다른 버튼들 활성화
				Log.d("tag", "키보드가 내려갔습니당~");
			}
		});
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d("tag","Resume");
		setAdditionalInfo(); //추가 유저정보 내용을 반영. finish 로 돌아올 때마다 반영하기 위해 onresume 에 넣음.
		setUserPhoto();
	}

	@Override
	public void onClick(View v) {
		String presentText;
		switch(v.getId()) {
		case R.id.btn_save_userinfo:
			if (isNetworkAvailableAndNotice()) {
				saveUserInfo();
			}
			break;
		case R.id.btn_save_userinfo_under:
			if (isNetworkAvailableAndNotice()) {
				saveUserInfo();
			}
			break;
		case R.id.userinfo_characters:
			TextView v_userinfo_characters = (TextView)findViewById(R.id.userinfo_characters);
			presentText = v_userinfo_characters.getText().toString();
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoEditAdditionalActivity.class);
			intentMoveToPage.putExtra("inputAddtionalKind", "characters");
			intentMoveToPage.putExtra("presentText", presentText);
			startActivity(intentMoveToPage);
			break;
		case R.id.userinfo_look:
			TextView v_userinfo_look = (TextView)findViewById(R.id.userinfo_look);
			presentText = v_userinfo_look.getText().toString();
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoEditAdditionalActivity.class);
			intentMoveToPage.putExtra("inputAddtionalKind", "look");
			intentMoveToPage.putExtra("presentText", presentText);
			startActivity(intentMoveToPage);
			break;
		case R.id.userinfo_hobby:
			TextView v_userinfo_hobby = (TextView)findViewById(R.id.userinfo_hobby);
			presentText = v_userinfo_hobby.getText().toString();
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoEditAdditionalActivity.class);
			intentMoveToPage.putExtra("inputAddtionalKind", "hobby");
			intentMoveToPage.putExtra("presentText", presentText);
			startActivity(intentMoveToPage);
			break;
		case R.id.userinfo_favorite:
			TextView v_userinfo_favorite = (TextView)findViewById(R.id.userinfo_favorite);
			presentText = v_userinfo_favorite.getText().toString();
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoEditAdditionalActivity.class);
			intentMoveToPage.putExtra("inputAddtionalKind", "favorite");
			intentMoveToPage.putExtra("presentText", presentText);
			startActivity(intentMoveToPage);
			break;
		case R.id.userinfo_idealtype:
			TextView v_userinfo_idealtype = (TextView)findViewById(R.id.userinfo_idealtype);
			presentText = v_userinfo_idealtype.getText().toString();
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoEditAdditionalActivity.class);
			intentMoveToPage.putExtra("inputAddtionalKind", "idealtype");
			intentMoveToPage.putExtra("presentText", presentText);
			startActivity(intentMoveToPage);
			break;
		case R.id.btn_show_album:
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoViewAlbumMineActivity.class);
			startActivity(intentMoveToPage);
			break;
		case R.id.userinfo_photo_01:
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoViewAlbumMineActivity.class);
			startActivity(intentMoveToPage);
			break;
		case R.id.userinfo_photo_02:
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoViewAlbumMineActivity.class);
			startActivity(intentMoveToPage);
			break;
		case R.id.userinfo_photo_03:
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoViewAlbumMineActivity.class);
			startActivity(intentMoveToPage);
			break;
		case R.id.userinfo_photo_04:
			intentMoveToPage = new Intent(UserinfoEditMainActivity.this,UserinfoViewAlbumMineActivity.class);
			startActivity(intentMoveToPage);
			break;
		case R.id.btn_goto_back:
			finish();
			break;
		}
		// TODO Auto-generated method stub
	}
	
	//화면 아무곳이나 클릭 했을 때 처리 (키보드 나왔을 때 처리 위한)
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
	    switch (ev.getAction()){
	        case MotionEvent.ACTION_DOWN:
	        	if (isShowKeyboard) {
		        	InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		        	mgr.hideSoftInputFromWindow(v_userinfo_introduce.getWindowToken(), 0);
		        	mgr.hideSoftInputFromWindow(v_userinfo_age.getWindowToken(), 0);
		        	mgr.hideSoftInputFromWindow(v_userinfo_job.getWindowToken(), 0);
		        	mgr.hideSoftInputFromWindow(v_userinfo_height.getWindowToken(), 0);
	        	}
	            break;
	        case MotionEvent.ACTION_UP:
	            break;
	    }
	    return super.dispatchTouchEvent(ev);
	}
	
	//유저 정보를 서버에서 받아옴
	private void getUserInfo() {
		progressDialog.setMessage("정보를 불러오고있습니다.");
		progressDialog.show();
		
		new Thread(new Runnable(){
			@Override
			public void run() {
				//UI 를 건들기 때문에 핸들러 사용.
				myHandler.post(new Runnable() {
					@Override
					public void run() {
						try {
							String jsonText = JsonUtils
									.getStringFromUrl(LovetoConstant.USERINFO_MINE_URL + "?memberid=" + memberid);
								Log.d("tag", jsonText);
							// 원격에서 읽어온 데이터로 JSON 객체 생성
							try {
								JSONArray jsonArray = new JSONArray(jsonText); 
								JSONObject object = jsonArray.getJSONObject(0);
								
								if (!"null".equals(object.getString("introduce"))) {
									v_userinfo_introduce.setText(object.getString("introduce"));
								}
								if (!"null".equals(object.getString("age"))) {
									v_userinfo_age.setText(object.getString("age"));
								}
								if (!"null".equals(object.getString("job"))) {
									v_userinfo_job.setText(object.getString("job"));
								}
								if (!"null".equals(object.getString("height"))) {
									v_userinfo_height.setText(object.getString("height"));
								}
								if (!"null".equals(object.getString("characters"))) {
									TextView v_userinfo_characters = (TextView) findViewById(R.id.userinfo_characters);
									v_userinfo_characters.setText(object.getString("characters"));
								}
								if (!"null".equals(object.getString("look"))) {
									TextView v_userinfo_look = (TextView) findViewById(R.id.userinfo_look);
									v_userinfo_look.setText(object.getString("look"));
								}
								if (!"null".equals(object.getString("hobby"))) {
									TextView v_userinfo_hobby = (TextView) findViewById(R.id.userinfo_hobby);
									v_userinfo_hobby.setText(object.getString("hobby"));
								}
								if (!"null".equals(object.getString("favorite"))) {
									TextView v_userinfo_favorite = (TextView) findViewById(R.id.userinfo_favorite);
									v_userinfo_favorite.setText(object.getString("favorite"));
								}
								if (!"null".equals(object.getString("idealtype"))) {
									TextView v_userinfo_idealtype = (TextView) findViewById(R.id.userinfo_idealtype);
									v_userinfo_idealtype.setText(object.getString("idealtype"));
								}
								
								//로컬 스피너 세팅
								beforeLocalName = object.getString("local");
								setLocalSpinner(object.getString("local"),object.getString("local_detail"));
								
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
						progressDialog.dismiss();
					}
				});
			}
		}).start();
	}

	private void saveUserInfo() {
		progressDialog.setMessage("정보를 서버에 저장하고 있습니다.");
		progressDialog.show();
		Runnable runnable = new Runnable(){
			@Override
			public void run() {
				//UI 를 건들기 때문에 핸들러 사용.
				myHandler.post(new Runnable() {
					@Override
					public void run() {
						local = spinnerLocal.getSelectedItem().toString();
						if (spinnerLocalDetail.isShown()) {
							local_detail = spinnerLocalDetail.getSelectedItem().toString();
						} else {
							local_detail = "";
						}
						if ("지역".equals(local)) {
							local = "";
						}
						if ("상세".equals(local_detail)) { 
							local_detail = "";
						}
						String introduce = v_userinfo_introduce.getText().toString();
						age = v_userinfo_age.getText().toString();
						job = v_userinfo_job.getText().toString();
						height = v_userinfo_height.getText().toString();
						TextView v_userinfo_characters = (TextView) findViewById(R.id.userinfo_characters);
						String characters = v_userinfo_characters.getText().toString();
						TextView v_userinfo_look = (TextView) findViewById(R.id.userinfo_look);
						String look = v_userinfo_look.getText().toString();
						TextView v_userinfo_hobby = (TextView) findViewById(R.id.userinfo_hobby);
						String hobby = v_userinfo_hobby.getText().toString();
						TextView v_userinfo_favorite = (TextView) findViewById(R.id.userinfo_favorite);
						String favorite = v_userinfo_favorite.getText().toString();
						TextView v_userinfo_idealtype = (TextView) findViewById(R.id.userinfo_idealtype);
						String idealtype = v_userinfo_idealtype.getText().toString();
						String gift_show_yn = "Y";
						Log.d("tag", "정보 추출 완료");
						
						//정보 추출 완료
						Log.d("tag", "Start to send info");
						try {
							//서버에 정보 보내기
							StringBuffer url = new StringBuffer("?memberid=");
							url.append(memberid).append("&local=").append(local).append("&local_detail=").append(local_detail);
							url.append("&age=").append(URLEncoder.encode(age,"UTF-8")).append("&job=").append(URLEncoder.encode(job,"UTF-8")).append("&height=").append(URLEncoder.encode(height,"UTF-8"));
							url.append("&gift_show_yn=").append(gift_show_yn).append("&characters=").append(URLEncoder.encode(characters,"UTF-8")).append("&look=").append(URLEncoder.encode(look,"UTF-8"));
							url.append("&hobby=").append(URLEncoder.encode(hobby,"UTF-8")).append("&favorite=").append(URLEncoder.encode(favorite,"UTF-8")).append("&idealtype=").append(URLEncoder.encode(idealtype,"UTF-8")).append("&introduce=").append(URLEncoder.encode(introduce,"UTF-8"));
							url.append("&phone_uid=").append(phone_uid);
							Log.d("tag", "url : " + url.toString());
							Log.d("tag", "LovetoConstant.USERINFO_EDIT_URL + urlStr : " + LovetoConstant.USERINFO_EDIT_URL + url.toString());
							String jsonText = JsonUtils.getStringFromUrl(LovetoConstant.USERINFO_EDIT_URL + url.toString());
							Log.d("tag", "jsonText : " + jsonText);
							//JSONArray jsonArray = new JSONArray(jsonText);
							JSONObject object = new JSONObject(jsonText);
							String result = object.getString("result");
							String err = object.getString("err");
							suspend_date = object.getString("suspend_date");
							suspend_reason = object.getString("suspend_reason");
							Log.d("tag", "result : " + result);
							Log.d("tag", "err : " + err);
							if ("T".equals(result)) {
								isSaveSuccess = true;
							} else {
								errorMsg = err;
							}
						} catch (JSONException e) {
							e.printStackTrace();
							Log.d("tag", "setRoundInfo() error, " + e.getMessage());
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
							Log.d("tag", "setRoundInfo() error, " + e.getMessage());
						} finally {
							if (isSaveSuccess) {
								saveToLocalDB();
								showSimpleAlertAndFinish("정보 저장에 성공했습니다.");
							} else if ("Incorrect PhoneID".equals(errorMsg)){
								//폰 인증정보가 틀린 경우
								showAlertIncorrectPhoneID(UserinfoEditMainActivity.this);
							} else if ("SuspendedID".equals(errorMsg)) {
								//이용 정지 상태인 경우
								showAlertSuspendID(UserinfoEditMainActivity.this, suspend_date, suspend_reason);
							} else {
								showSimpleAlert("데이터 저장에 실패했습니다. 다시 시도해주세요.");
							}
							progressDialog.dismiss();
						}
						Log.d("tag", "saveTournamentOpenInfoToServer() end");

					} //Handler run End
				});
			}
		};
		new Thread(runnable).start();
	}
	
	//로컬에 저장 된 추가정보 수정 내용을 반영함.
	private void setAdditionalInfo() {
		String input_characters = pref.getString("input_characters", "");
		String input_look = pref.getString("input_look", "");
		String input_hobby = pref.getString("input_hobby", "");
		String input_favorite = pref.getString("input_favorite", "");
		String input_idealtype = pref.getString("input_idealtype", "");
		Editor editor = pref.edit();
		if (!"".equals(input_characters)) {
			TextView v_userinfo_characters = (TextView)findViewById(R.id.userinfo_characters);
			v_userinfo_characters.setText(input_characters);
			editor.putString("input_characters", ""); //한 번 변경 후에는 다시 변경하지 않도록 함.
		}
		if (!"".equals(input_look)) {
			TextView v_userinfo_look = (TextView)findViewById(R.id.userinfo_look);
			v_userinfo_look.setText(input_look);
			editor.putString("input_look", ""); //한 번 변경 후에는 다시 변경하지 않도록 함.
		}
		if (!"".equals(input_hobby)) {
			TextView v_userinfo_hobby = (TextView)findViewById(R.id.userinfo_hobby);
			v_userinfo_hobby.setText(input_hobby);
			editor.putString("input_hobby", ""); //한 번 변경 후에는 다시 변경하지 않도록 함.
		}
		if (!"".equals(input_favorite)) {
			TextView v_userinfo_favorite = (TextView)findViewById(R.id.userinfo_favorite);
			v_userinfo_favorite.setText(input_favorite);
			editor.putString("input_favorite", ""); //한 번 변경 후에는 다시 변경하지 않도록 함.
		}
		if (!"".equals(input_idealtype)) {
			TextView v_userinfo_idealtype = (TextView)findViewById(R.id.userinfo_idealtype);
			v_userinfo_idealtype.setText(input_idealtype);
			editor.putString("input_idealtype", ""); //한 번 변경 후에는 다시 변경하지 않도록 함.
		}
		editor.commit();
	}
	
	
	@Override
	public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
	// TODO Auto-generated method stub
		Log.d("tag","스피너 선택 함");
		String strLocal = LovetoConstant.LOCAL_NAME[position];
		
		if (!("".equals(beforeLocalName))&&!(strLocal.equals(beforeLocalName))) {	//액티비티 시작하자 마자 구동 되는 것에대한 회피
			Log.d("tag","local: " + strLocal);
			Log.d("tag","beforeLocalName: " + beforeLocalName);
			setLocalSpinner(strLocal,"상세");
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		Log.d("tag","아무것도 선택되지 않았슴다");
		return;
	}

	private void setLocalSpinner(String local,String local_detail) {
		//DB 에 저장된 local 명으로 스피너 세팅
		for (int i=0;i<LovetoConstant.LOCAL_NAME.length;i++) {
			if (local.equals(LovetoConstant.LOCAL_NAME[i])) {
				presentLocalNum = i;
				spinnerLocal.setSelection(i);
				Log.d("tag","지역은 :"+LovetoConstant.LOCAL_NAME[i]);
			}
		}
		// DB 에 저장된 local_detail 명으로 스피너 세팅
		if (presentLocalNum > 8) { //local 9번(강원) 부터 상세 지역 표시
			switch(presentLocalNum) {
			case 9:
				arrLocalDetail = LovetoConstant.LOCALDETAIL_KANGWON;
				break;
			case 10:
				arrLocalDetail = LovetoConstant.LOCALDETAIL_KYOUNGKI;
				break;
			case 11:
				arrLocalDetail = LovetoConstant.LOCALDETAIL_KYOUNGNAM;
				break;
			case 12:
				arrLocalDetail = LovetoConstant.LOCALDETAIL_KYOUNGBOOK;
				break;
			case 13:
				arrLocalDetail = LovetoConstant.LOCALDETAIL_JUNNAM;
				break;
			case 14:
				arrLocalDetail = LovetoConstant.LOCALDETAIL_JUNBOOK;
				break;
			case 15:
				arrLocalDetail = LovetoConstant.LOCALDETAIL_CHOONGNAM;
				break;
			case 16:
				arrLocalDetail = LovetoConstant.LOCALDETAIL_CHOONGBOOK;
				break;
			}
			localDetailAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,arrLocalDetail); //스피너 어댑터
			localDetailAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerLocalDetail.setAdapter(localDetailAdapter);
			localDetailAdapter.notifyDataSetChanged();
			for (int i=0;i < arrLocalDetail.length;i++) {
				if (local_detail.equals(arrLocalDetail[i])) {
					spinnerLocalDetail.setSelection(i);
					Log.d("tag","상세지역은 :"+arrLocalDetail[i]);
				}
			}
			Log.d("tag","선택 된 값은 :"+spinnerLocalDetail.getSelectedItem().toString());
			spinnerLocalDetail.setVisibility(Spinner.VISIBLE); //local 9번부터는 상세지역 스피너 표시
		} else {
			spinnerLocalDetail.setVisibility(Spinner.GONE); //local 1~8번은 상세지역 스피너 숨김
		}
	}
	
	//키보드 표시 유무를 false 로 만듬. 딜레이를 주지 않으면 클릭 처리가 바로 되버림.
	private void reActiveButtons() { 
		new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
            	isShowKeyboard = false;
            	Log.d("tag", "isShowKeyboard :" + isShowKeyboard);
            }
        }, 300);
	}
	
	private void saveToLocalDB() {
		Editor editor = pref.edit();
		editor.putString("local", local);
		editor.putString("local_detail", local_detail);
		editor.putString("age", age);
		editor.putString("job", job);
		editor.putString("height", height);
		editor.commit();
	}
	
	private void setUserPhoto() {
		String photo_url1 = pref.getString("photo_url1", "");
		String photo_url2 = pref.getString("photo_url2", "");
		String photo_url3 = pref.getString("photo_url3", "");
		String photo_url4 = pref.getString("photo_url4", "");
		
		ImageView v_photo_01 = (ImageView)findViewById(R.id.userinfo_photo_01);
		ImageView v_photo_02 = (ImageView)findViewById(R.id.userinfo_photo_02);
		ImageView v_photo_03 = (ImageView)findViewById(R.id.userinfo_photo_03);
		ImageView v_photo_04 = (ImageView)findViewById(R.id.userinfo_photo_04);
		
		String photoUrl;
    	ImageDownloader imageDownloader = new ImageDownloader();
		if ("".equals(photo_url1) || "null".equals(photo_url1)) {
			v_photo_01.setImageResource(R.drawable.icon_userinfo_addphoto);
			v_photo_01.setScaleType(ImageView.ScaleType.CENTER);
		} else {
			photoUrl = LovetoConstant.PHOTO_URL + memberid + "_1.jpg";
			v_photo_01.setScaleType(ImageView.ScaleType.FIT_XY);
			imageDownloader.download(photoUrl, v_photo_01);
		}
		if ("".equals(photo_url2) || "null".equals(photo_url2)) {
			v_photo_02.setImageResource(R.drawable.icon_userinfo_addphoto);
			v_photo_02.setScaleType(ImageView.ScaleType.CENTER);
		} else {
			photoUrl = LovetoConstant.PHOTO_URL + memberid + "_2.jpg";
			v_photo_02.setScaleType(ImageView.ScaleType.FIT_XY);
			imageDownloader.download(photoUrl, v_photo_02);
		}
		if ("".equals(photo_url3) || "null".equals(photo_url3)) {
			v_photo_03.setImageResource(R.drawable.icon_userinfo_addphoto);
			v_photo_03.setScaleType(ImageView.ScaleType.CENTER);
		} else {
			photoUrl = LovetoConstant.PHOTO_URL + memberid + "_3.jpg";
			v_photo_03.setScaleType(ImageView.ScaleType.FIT_XY);
			imageDownloader.download(photoUrl, v_photo_03);
		}
		if ("".equals(photo_url4) || "null".equals(photo_url4)) {
			v_photo_04.setImageResource(R.drawable.icon_userinfo_addphoto);
			v_photo_04.setScaleType(ImageView.ScaleType.CENTER);
		} else {
			photoUrl = LovetoConstant.PHOTO_URL + memberid + "_4.jpg";
			v_photo_04.setScaleType(ImageView.ScaleType.FIT_XY);
			imageDownloader.download(photoUrl, v_photo_04);
		}
		
	}
	
}
